<!-- Buttons to add to the header. -->
<core-navbar-buttons end>
    <core-context-menu>
        <core-context-menu-item *ngIf="externalUrl" [priority]="900" [content]="'core.openinbrowser' | translate" [href]="externalUrl" [iconAction]="'open'"></core-context-menu-item>
        <core-context-menu-item *ngIf="description" [priority]="800" [content]="'core.moduleintro' | translate" (action)="expandDescription()" [iconAction]="'arrow-forward'"></core-context-menu-item>
        <core-context-menu-item *ngIf="blog" [priority]="750" content="{{'addon.blog.blog' | translate}}" [iconAction]="'fa-newspaper-o'" (action)="gotoBlog($event)"></core-context-menu-item>
        <core-context-menu-item *ngIf="loaded && !(hasOffline || hasOfflineRatings) && isOnline" [priority]="700" [content]="'addon.mod_forum.refreshdiscussions' | translate" (action)="doRefresh(null, $event)" [iconAction]="refreshIcon" [closeOnClick]="false"></core-context-menu-item>
        <core-context-menu-item *ngIf="loaded && (hasOffline || hasOfflineRatings) && isOnline"  [priority]="600" [content]="'core.settings.synchronizenow' | translate" (action)="doRefresh(null, $event, true)" [iconAction]="syncIcon" [closeOnClick]="false"></core-context-menu-item>
        <core-context-menu-item *ngIf="prefetchStatusIcon" [priority]="500" [content]="prefetchText" (action)="prefetch($event)" [iconAction]="prefetchStatusIcon" [closeOnClick]="false"></core-context-menu-item>
        <core-context-menu-item *ngIf="size" [priority]="400" [content]="'core.removefiles' | translate:{$a: size}" [iconDescription]="'cube'" (action)="removeFiles()" [iconAction]="'trash'"></core-context-menu-item>
        <core-context-menu-item *ngIf="sortingAvailable" [priority]="300" [content]="'core.sort' | translate" (action)="showSortOrderSelector($event)" iconAction="fa-sort"></core-context-menu-item>
    </core-context-menu>
</core-navbar-buttons>

<!-- Content. -->
<core-split-view>
    <ion-content>
        <ion-refresher [enabled]="loaded" (ionRefresh)="doRefresh($event)">
            <ion-refresher-content pullingText="{{ 'core.pulltorefresh' | translate }}"></ion-refresher-content>
        </ion-refresher>
        <core-loading [hideUntil]="loaded" class="core-loading-center">
            <core-course-module-description *ngIf="forum && forum.type != 'single'" [description]="description" [component]="component" [componentId]="componentId" [note]="descriptionNote" contextLevel="module" [contextInstanceId]="module.id" [courseId]="courseId"></core-course-module-description>

            <!-- Forum discussions found to be synchronized -->
            <ion-card class="core-warning-card" icon-start *ngIf="hasOffline || hasOfflineRatings">
                <ion-icon name="warning"></ion-icon> {{ 'core.hasdatatosync' | translate:{$a: moduleName} }}
            </ion-card>

            <!-- Cut-off date or due date message -->
            <ion-card class="core-info-card" icon-start *ngIf="availabilityMessage">
                <ion-icon name="information-circle"></ion-icon> {{ availabilityMessage }}
            </ion-card>

            <ng-container *ngIf="forum">
                <core-empty-box *ngIf="discussions.length == 0 && offlineDiscussions.length == 0" icon="chatbubbles" [message]="'addon.mod_forum.forumnodiscussionsyet' | translate">
                </core-empty-box>

                <div text-wrap *ngIf="discussions.length > 0 && sortingAvailable && selectedSortOrder" ion-row padding-horizontal padding-top margin-bottom>
                    <button *ngIf="sortingAvailable" ion-button padding-horizontal icon-end ion-col (click)="showSortOrderSelector($event)" color="light" class="core-button-select button-no-uppercase" [attr.aria-label]="('core.sort' | translate)" aria-haspopup="true" [attr.aria-expanded]="sortOrderSelectorExpanded" aria-controls="addon-mod-forum-sort-order-selector" id="addon-mod-forum-sort-order-button">
                        <span class="core-button-select-text">{{ selectedSortOrder.label | translate }}</span>
                        <ion-icon name="arrow-dropdown" ios="md-arrow-dropdown"></ion-icon>
                    </button>
                </div>

                <ng-container *ngFor="let discussion of offlineDiscussions">
                    <ion-item text-wrap (click)="openNewDiscussion(discussion.timecreated)" [attr.no-lines]="discussion.groupname" [class.core-split-item-selected]="discussion.timecreated == -selectedDiscussion" class="addon-mod-forum-discussion">
                        <div class="addon-mod-forum-discussion-title">
                            <h2>
                                <core-format-text [text]="discussion.subject" contextLevel="module" [contextInstanceId]="module.id" [courseId]="courseId"></core-format-text>
                            </h2>
                        </div>
                        <div class="addon-mod-forum-discussion-info">
                            <ion-avatar core-user-avatar [user]="discussion" item-start [courseId]="courseId" *ngIf="discussion.userfullname"></ion-avatar>
                            <div class="addon-mod-forum-discussion-author">
                                <h3 *ngIf="discussion.userfullname">{{discussion.userfullname}}</h3>
                                <p *ngIf="discussion.groupname"><ion-icon name="people"></ion-icon> {{ discussion.groupname }}</p>
                                <p><ion-icon name="time"></ion-icon> {{ 'core.notsent' | translate }}</p>
                            </div>
                        </div>
                    </ion-item>
                </ng-container>
                <ng-container *ngFor="let discussion of discussions">
                    <ion-item (click)="openDiscussion(discussion)" [class.core-split-item-selected]="discussion.discussion == selectedDiscussion" class="addon-mod-forum-discussion">
                        <div class="addon-mod-forum-discussion-title">
                            <h2 text-wrap>
                                <core-icon name="fa-map-pin" *ngIf="discussion.pinned"></core-icon>
                                <core-icon name="fa-star" class="addon-forum-star" *ngIf="!discussion.pinned && discussion.starred"></core-icon>
                                <core-format-text [text]="discussion.subject" contextLevel="module" [contextInstanceId]="module.id" [courseId]="courseId"></core-format-text>
                            </h2>
                            <button ion-button icon-only clear color="dark" (click)="showOptionsMenu($event, discussion)" *ngIf="canPin || discussion.canlock || discussion.canfavourite">
                                <core-icon name="more"></core-icon>
                            </button>
                        </div>
                        <div class="addon-mod-forum-discussion-info">
                            <ion-avatar *ngIf="discussion.userfullname" core-user-avatar [user]="discussion" item-start [courseId]="courseId"></ion-avatar>
                            <div class="addon-mod-forum-discussion-author">
                                <h3 *ngIf="discussion.userfullname">{{discussion.userfullname}}</h3>
                                <p *ngIf="discussion.groupname"><ion-icon name="people"></ion-icon> {{ discussion.groupname }}</p>
                                <p>{{discussion.created * 1000 | coreFormatDate: "strftimerecentfull"}}</p>
                            </div>
                        </div>
                        <ion-row text-center class="addon-mod-forum-discussion-more-info">
                            <ion-col text-start>
                                <ion-note>
                                    <ion-icon name="time"></ion-icon> {{ 'addon.mod_forum.lastpost' | translate }}
                                    <ng-container *ngIf="discussion.timemodified > discussion.created">{{discussion.timemodified | coreTimeAgo}}</ng-container>
                                    <ng-container *ngIf="discussion.timemodified <= discussion.created">{{discussion.created | coreTimeAgo}}</ng-container>
                                </ion-note>
                            </ion-col>
                            <ion-col text-end>
                                <ion-note>
                                    <ion-icon name="chatboxes"></ion-icon> {{ 'addon.mod_forum.numreplies' | translate:{numreplies: discussion.numreplies} }}
                                    <ion-badge text-center *ngIf="discussion.numunread" [attr.aria-label]="'addon.mod_forum.unreadpostsnumber' | translate:{ '$a' : discussion.numunread}">{{ discussion.numunread }}</ion-badge>
                                </ion-note>
                            </ion-col>
                        </ion-row>
                    </ion-item>
                </ng-container>

                <core-infinite-loading [enabled]="canLoadMore" (action)="fetchMoreDiscussions($event)" [error]="loadMoreError"></core-infinite-loading>
            </ng-container>
        </core-loading>

        <ion-fab core-fab bottom end *ngIf="forum && canAddDiscussion">
            <button ion-fab (click)="openNewDiscussion()" [attr.aria-label]="addDiscussionText">
                <ion-icon name="add"></ion-icon>
            </button>
        </ion-fab>
    </ion-content>
</core-split-view>
